#include    "ResponsableEtu.h"

/** @brief recListNumEtuParEns
  *
  * @todo: document this function
  */
TabEtu* recListEtuParEns(Ens * ens)
{
    assert(ens!=NULL);
    return  recListEtuParNumEns(ens->numEns);
}

/** @brief recRespEtuParNumEtu
  *
  * @todo: document this function
  */
RespEtu* recRespEtuParNumEtu(int numEtu)
{
    FILE* f;
    f = fopen(RESPETU_DB, "r");

    if (f==NULL)
        {
        printf("Erreur lors de l'ouverture de %s\n", RESPETU_DB);
        exit(EXIT_FAILURE);
        }
     char* contenu=calloc(100,sizeof(char));
    while(fgets(contenu,100,f)!=NULL)
    {
       char**tabor=g_strsplit((gchar*)contenu,(gchar*)"\n",100);
        char**tab=g_strsplit((gchar*)tabor[0],(gchar*)":",100);
        if(numEtu == atoi(tab[0]))
        {
            fclose(f);
            return creRespEtu(atoi(tab[0]),atoi(tab[1]));
        }
    }    if(!feof(f))
        printf("autre probleme \n");
    rewind(f);
    fclose(f);
    return NULL;
}

/** @brief recRespEtu
  *
  * @todo: document this function
  */
RespEtu* recRespEtu(Etu * etu)
{
    assert(etu!=NULL);
    return  recRespEtuParNumEtu(etu->numEtu);
}

/** @brief modfRespEtu
  *
  * @todo: document this function
  */
void modfRespEtu(RespEtu *respEtu,RespEtu * respEtuNew)
{
    supRespEtu(respEtu);
    saveRespEtu(respEtuNew);
}

/** @brief supRespEtuParId
  *
  * @todo: document this function
  */
int supRespEtuParIdEtu(int idEtu)
{
       char *contenu=calloc(100,sizeof(char));
    FILE * fIn;
    FILE * fOut;

    if ((fIn = fopen(RESPETU_DB, "r")) == NULL)
        return EXIT_FAILURE;

    if ((fOut = fopen("texte.tmp", "w")) == NULL)
    {
        fclose(fIn);
        return EXIT_FAILURE;
    }

    while (fgets(contenu,100, fIn))
    {
        char * ligne=calloc(100,sizeof(char));
        copStr(&ligne,contenu);
        char**tabor=g_strsplit((gchar*)contenu,(gchar*)"\n",100);
        char**tab=g_strsplit((gchar*)tabor[0],(gchar*)":",100);
        if (atoi(tab[0]) != idEtu)
                    fputs(ligne, fOut);
    }

    fclose(fIn);
    fclose(fOut);

    rename("texte.tmp", RESPETU_DB);
      return 0;
}

/** @brief supRespEtu
  *
  * @todo: document this function
  */
int supRespEtu(RespEtu * respEtu)
{
    assert(respEtu!=NULL);
    return  supRespEtuParIdEtu(respEtu->idEtu);
}

/** @brief saveRespEtuParId
  *
  * @todo: document this function
  */
void saveRespEtuParId(int idEtu,int idEns)
{
 FILE* f;

    f = fopen(RESPETU_DB, "a");

    if (f==NULL)
        {
        printf("Erreur lors de l'ouverture de %s\n", UE_DB);
        exit(EXIT_FAILURE);
        }

    if(recRespEtuParNumEtu(idEtu) == NULL)
    {


        char*cidEtu=calloc(20,sizeof(char));
        char*cidEns=calloc(20,sizeof(char));
        sprintf(cidEtu,"%d",idEtu);
        sprintf(cidEns,"%d",idEns);
        char*chaine=calloc(100,sizeof(char));

        strcat(chaine,cidEtu);
        strcat(chaine,":");
        strcat(chaine,cidEns);
        strcat(chaine,"\n");

        fputs(chaine, f);
    }
    else
        printf("Un referant existe deja dans la base de donnée!!!!!\n");

    fclose(f);
}

/** @brief saveRespEtu
  *
  * @todo: document this function
  */
void saveRespEtu(RespEtu * respEtu)
{
    assert(respEtu!=NULL);
    saveRespEtuParId(respEtu->idEtu,respEtu->idEns);
}

/** @brief initRespEtu
  *
  * @todo: document this function
  */
void initRespEtu(RespEtu * respEtu, int idEtu,int idEns)
{
    assert(respEtu!=NULL);
respEtu->idEtu=idEtu;
respEtu->idEns=idEns;
}

/** @brief creRespEtu
  *
  * @todo: document this function
  */
RespEtu* creRespEtu(int idEtu,int idEns)
{
RespEtu* respEtu=calloc(1,sizeof(RespEtu));
initRespEtu(respEtu,idEtu,idEns);
return  respEtu;
}

/** @brief recListNumEtuParNumEns
  *
  * @todo: document this function
  */
TabEtu* recListEtuParNumEns(int numEns)
{
    TabEtu  *   listEtu=calloc(1,sizeof(TabEtu));
    Etu **  etus=calloc(1,sizeof(Etu));
    int i=1;

     FILE *f=NULL;
    f = fopen(RESPETU_DB, "r");

    if (f==NULL)
        {
        printf("Erreur lors de l'ouverture de %s\n", RESPETU_DB);
        exit(EXIT_FAILURE);
        }
     char* contenu=calloc(100,sizeof(char));
    while(fgets(contenu,100,f)!=NULL)
    {
      char**tabor=g_strsplit((gchar*)contenu,(gchar*)"\n",100);
        char**tab=g_strsplit((gchar*)tabor[0],(gchar*)":",100);
        if(numEns == atoi(tab[1]))
        {
            etus=realloc(etus,sizeof(Etu)*i);
            etus[i-1]=recEtuParCode(atoi(tab[0]));
            i++;
        }


    }


    listEtu->nbEtu=i-1;
    listEtu->tabEtu=etus;

    rewind(f);
    fclose(f);

    return listEtu;


}

